var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.urlimg,
    shortcut: false,//判断模态框的显示与隐藏
    user_receive: 0,//判断用户是否通过认证
    head_num: 0,
    discounts: [
    ],
    reducefund_lists: [  //show  是否展示(true false)    receive  是否领取(true false)  receive_num 领取次数
    ],
    images:[],
  },
  //头部点击
  head_click: function (e) {
    var that = this;
    that.setData({
      head_num: e.currentTarget.dataset.index,
    })
  },
  //关闭认证模态框
  down_mode:function(){
    this.setData({
      shortcut: false,
    })
  },

  //获取用户输入的电弧号码(正则认证)
  formSubmit: function (e) {
    var that = this;
    var value=e.detail.value.phon_input;
    if ((/^1[34578]\d{9}$/.test(value))) {
       wx.request({
         url: app.url + "api_team/memberVerify",
         header: {
           'content-type': 'application/json',
           'token': wx.getStorageSync("token_key")
         },
         data: { mobile: value },//mobile: phoneNum
         success: function(res) {
         
           var orderList = res.data.result.order_list;
           console.log(res.data.result.order_list);
           var totalMoney = 0;
           for (var i = 0; i < orderList.length; i++) {
             if (orderList[i].money_paid != null) {
               totalMoney += parseFloat(orderList[i].money_paid);
             }
           }
           var shortcut = that.data.shortcut;
           var user_receive=0;
           if (totalMoney > 59) {
              shortcut = false;
              user_receive=1;
              wx.showToast({
                title: '认证成功',  //标题  
                icon: 'success',
                duration: 1000,
              }) 
                
           } else {
              wx.showToast({
                title: '认证失败',  //标题  
                icon: 'loading',
                duration: 1000,
              }) 
                shortcut = true;
                user_receive = 0;
           }
           that.setData({ shortcut: shortcut, user_receive: user_receive, })
         },
         fail: function(res) {
          
         },
         complete: function(res) {},
       })

    }else{
      wx.showToast({
        title: '请输入您的手机号码',  //标题  
        icon: 'loading',
        duration: 1000, 
      }) 
    }
  },


  //领取立减金
  receive_click: function (e) {
    var that = this;
    var user_receive = that.data.user_receive;
    var shortcut = that.data.shortcut;
    if (user_receive == 1) {//如果用户通过认证

    var couponId = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var reducefund_lists = that.data.reducefund_lists;
    console.log(reducefund_lists);
    wx.request({
      url: app.url + "api_coupon/receive",
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync("token_key")
      },
      data: {
        couponId: couponId,
        type: 1
      },
      success: function (result) {
        console.log(result);
        if (result.data.result != 'Flase') {
          //console.log(reducefund_lists[index]);
          //console.log(reducefund_lists[index].restricts);
          reducefund_lists[index].restricts--;
          if (reducefund_lists[index].restricts > 0) {
            //提示信息
            wx.showToast({
              title: '还能领取' + reducefund_lists[index].restricts + '次',
              icon: 'success',
              duration: 1000,
              success: function () {
                that.setData({
                  reducefund_lists: reducefund_lists,
                })
              },
            })
          } else {
            reducefund_lists[index].receive = false;
            wx.showToast({
              title: '领取成功',
              icon: 'loading',
              duration: 1200,
              success: function () {
                that.setData({
                  reducefund_lists: reducefund_lists,
                })
              },
            })
          }

        } else {
          //提示信息
          wx.showToast({
            title: '您已领取过了',
            icon: 'loading',
            duration: 1200,
          })
        }
      }
    })
      function prompt_title(title, icon) {
        //提示信息
        wx.showToast({
          title: title,
          icon: icon,
          duration: 1200,
          mask: true,
        })
      }
      that.setData({
        //reducefund_lists: reducefund_lists,
      })


    } else {//如果用户还没有通过认证


      console.log('用户还没有通过认证,进行认证');
      that.setData({
        shortcut: true,
      })



    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token_key = wx.getStorageSync('token_key');
    console.log(token_key);
    if (token_key == undefined || token_key == '') {
      app.getUserInfo(function () {
        load_fn();
      })
    } else {
      load_fn()
    }
    function load_fn() {
      //立减金请求
      wx.request({
        url: app.url + "api_coupon/reducefundlist",
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync("token_key")
        }, success: function (json) {
          console.log(json.data.result.adlist);
          that.setData({
            reducefund_lists: json.data.result.couponList,
            coupon_banner: json.data.result.adlist,
          });
        }
      })

      //广告模板请求
      wx.request({
        url: app.url + "api_goods/indexTopic",
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync("token_key")
        }, success: function (json) {
          console.log(json.data);
          that.setData({
            picture: json.data.result,
          })
        }
      })

    }
  },
  //广告位图片大小控制
  imageLoad: function (e) {
    console.log(e);
    console.log(e.currentTarget.dataset.id);
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 718 / ratio;    //计算的高度值
    var image = this.data.images;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image[e.currentTarget.dataset.id] = {
      width: viewWidth,
      height: viewHeight
    }
    console.log(image);
    this.setData({
      images: image
    })
  },
  // 立减金专区的广告模板点击事件
  advertising_click: function (e) {
    console.log(e.currentTarget.dataset.url);
    var url = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})